﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FATCATool
{
    public partial class AccountReportControl : UserControl
    {
        private CorrectableAccountReport_Type _report;
        private bool _bindingComplete = false;

        public AccountReportControl()
        {
            InitializeComponent();
        }

        public void Initialize(CorrectableAccountReport_Type report)
        {
            tabControl1.TabPages.Remove(tabInividual);
            tabControl1.TabPages.Remove(tabOrganization);

            _bindingComplete = false;
            _report = report;
            docSpecControl.Intitalize(_report.DocSpec);
            DataBind();
            _bindingComplete = true;
        }

        private void DataBind()
        {
            if (_report.AccountHolder.Items[0] is PersonParty_Type)
            {
                radIndividual.Checked = true;
                var person = (PersonParty_Type)_report.AccountHolder.Items[0];

                personTypeControl.Intitalize(person);
            }
            else
            {
                radOrganization.Checked = true;

                var org = new FATCASchema.Wrappers.OrgPartyWithType();
                org.SetValues(_report.AccountHolder.Items);


                cmbAcctHolderType.DataBindings.Clear();
                cmbAcctHolderType.DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(FatcaAcctHolderType_EnumType));
                cmbAcctHolderType.ValueMember = "Key";
                cmbAcctHolderType.DisplayMember = "Value";
                cmbAcctHolderType.DataBindings.Add("SelectedItem", org, "AccountType");

                correctableOganizationControl.Intitalize(org.Organization);
            }
            SetTabs();

            numAmount.DataBindings.Clear();
            numAmount.DataBindings.Add("Value", _report.AccountBalance, "Value");

            txtAccountNumber.DataBindings.Clear();
            txtAccountNumber.DataBindings.Add("Text", _report, "AccountNumber");

            cmbCurrency.DataBindings.Clear();
            cmbCurrency.DataSource = Enum.GetValues(typeof(currCode_Type));
            cmbCurrency.DataBindings.Add("SelectedItem", _report.AccountBalance, "currCode");

            ((DataGridViewComboBoxColumn)grdPayments.Columns["paymentTypeDataGridViewTextBoxColumn"]).DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(FatcaPaymentType_EnumType));
            ((DataGridViewComboBoxColumn)grdPayments.Columns["paymentTypeDataGridViewTextBoxColumn"]).ValueMember = "Key";
            ((DataGridViewComboBoxColumn)grdPayments.Columns["paymentTypeDataGridViewTextBoxColumn"]).DisplayMember = "Value";

            ((DataGridViewComboBoxColumn)grdPayments.Columns["currencyDataGridViewTextBoxColumn"]).DataSource = Enum.GetValues(typeof(currCode_Type));

            if (_report.Payment != null)
                paymentBindingSource.DataSource = FATCASchema.Wrappers.Payments.GetWrapperAddresses(_report.Payment);
            else
                paymentBindingSource.DataSource = new FATCASchema.Wrappers.Payments();
        }

        private void paymentBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (paymentBindingSource.DataSource is FATCASchema.Wrappers.Payments)
            {
                var payments = (FATCASchema.Wrappers.Payments)paymentBindingSource.DataSource;
                if (payments.Count > 0)
                    _report.Payment = payments.ToFATCAArray();
                else
                    _report.Payment = null;
            }
        }

        private void radIndividual_CheckedChanged(object sender, EventArgs e)
        {
            SetTabs();
        }

        private void SetTabs()
        {
            if (radIndividual.Checked)
            {
                if (_report.AccountHolder.Items == null || _report.AccountHolder.Items.Count() == 0 || !(_report.AccountHolder.Items[0] is PersonParty_Type))
                {
                    _report.AccountHolder.Items = FATCA_OECD.CreateNewReportingIndividual();
                }

                tabControl1.TabPages.Remove(tabOrganization);
                if (!tabControl1.TabPages.Contains(tabInividual))
                    tabControl1.TabPages.Add(tabInividual);
            }
            else
            {
                if (_report.AccountHolder.Items == null || _report.AccountHolder.Items.Count() == 0 || (!(_report.AccountHolder.Items[0] is OrganisationParty_Type) && !(_report.AccountHolder.Items[0] is FatcaAcctHolderType_EnumType)))
                {
                    _report.AccountHolder.Items = FATCA_OECD.CreateNewReportingOrganization();
                }

                tabControl1.TabPages.Remove(tabInividual);
                if (!tabControl1.TabPages.Contains(tabOrganization))
                    tabControl1.TabPages.Add(tabOrganization);
            }
        }
    }
}
